import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm'
import { Operate } from './operate.entity'
import { Role } from './role.entity'
import { User } from './user.entity'

@Entity()
export class UserRole extends Operate {
  @Column({ name: 'user_id', type: 'bigint', comment: '用户id' })
  userId: string

  @Column({ name: 'role_id', type: 'bigint', comment: '角色id' })
  roleId: string

  @ManyToOne(() => User, (user) => user.roles, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'user_id', foreignKeyConstraintName: 'role_id' })
  user: User

  @ManyToOne(() => Role, (role) => role.users, {
    cascade: true,
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'role_id', foreignKeyConstraintName: 'user_id' })
  role: Role
}
